﻿using System;
using System.Collections.Generic;

namespace StudySystem.MainLogic.Others
{
    //此类用来写一些通用的功能性算法
    public static class Utilities
    {
        /// <summary>
        /// 从list中选取count个元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">原list</param>
        /// <param name="count">要选取的元素数量</param>
        /// <param name="affectFrom">是否对原list产生修改，即被选择的元素从原list中剔除</param>
        /// <returns></returns>
        public static List<T> Random<T>(List<T> list, int count, bool affectFrom)
        {
            if (list == null)
                return new List<T>();
            List<T> tmp = null;
            if (affectFrom)
            {
                tmp = list;
            }
            else
            {
                tmp = new List<T>();
                tmp.AddRange(list);
            }

            int length = list.Count;
            Random rd = new Random();
            if (count < 0)
                count = length;
            else
                count = Math.Min(count, length);
            List<T> ret = new List<T>();
            for (int i = 0; i < count; ++i)
            {
                // 随机获取下标
                int n = rd.Next(tmp.Count);
                // 将item加入返回数组
                ret.Add(tmp[n]);
                //在tmp中删除当前对象
                tmp.Remove(tmp[n]);
            }
            return ret;
        }



        public static int Sum(int[] array)
        {
            int ret = 0;
            foreach (int i in array)
            {
                ret += i;
            }
            return ret;
        }
        
    }
}
